package com.zrc.module_my.fragment

import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ToastUtils
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.zrc.superframe_base.ARouterHub
import com.zrc.superframe_base.mvx.mvc.BaseMvcFragment
import com.zrc.module_my.IConstantPool
import com.zrc.module_my.MyApi
import com.zrc.module_my.R
import com.zrc.module_my.bean.BriefListBean
import com.zrc.superframe_net.common.CacheConfig
import com.zrc.superframe_net.common.net.ApplySchedulers
import com.zrc.superframe_net.common.net.NetObserver
import com.zrc.superframe_net.common.net.Net
import com.zrc.superframe_net.common.net.NetCallBack
import com.zrc.superframe_net.common.LoadingConfig
import com.zrc.superframe_net.room.NetRoomUtil
import kotlinx.android.synthetic.main.fragment_net.*
import okhttp3.ResponseBody
import java.io.IOException

/**
 * @Author: superman
 * @CreateTime: 6/27/21
 * @Describe:
 */
@Route(path = ARouterHub.FRAGMENT_NET, name = "网络请求案例")
class NetFragment : BaseMvcFragment() {

    override fun getLayoutID() = R.layout.fragment_net

    override fun initListener() {
        super.initListener()
        bt_request.setOnClickListener(this)
        bt_request_gson.setOnClickListener(this)
        bt_request_gson_dialog.setOnClickListener(this)
        bt_request_gson_un_dialog.setOnClickListener(this)
        bt_request_gson_buffer.setOnClickListener(this)
        bt_request_gson_buffer_custom.setOnClickListener(this)
        bt_request_all.setOnClickListener(this)
    }

    override fun onClick(view: View) {
        super.onClick(view)
        when (view) {
            bt_request -> {//普通请求，返回String 自己解析的这种
                Net.getService(MyApi::class.java)
                    .requestList()
                    .compose(ApplySchedulers())
                    .subscribe(
                        NetObserver(
                            page = this,
                            loadingConfig = this.context?.let { LoadingConfig(it) },
                            callback = object : NetCallBack<ResponseBody>() {
                                override fun onSucceed(body: ResponseBody, s: String) {
                                    try {
                                        ToastUtils.showShort(body.string())
                                    } catch (e: IOException) {
                                        e.printStackTrace()
                                    }
                                }
                            })
                    )
            }
            bt_request_gson -> {//普通请求，返回Gson
                Net.getService(MyApi::class.java)
                    .requestList_GSON()
                    .compose(ApplySchedulers())
                    .subscribe(
                        NetObserver(
                            page = this,
                            loadingConfig = this.context?.let { LoadingConfig(it) },
                            callback = object : NetCallBack<List<BriefListBean>>() {
                                override fun onSucceed(
                                    briefListBeen: List<BriefListBean>,
                                    s: String
                                ) {
                                    ToastUtils.showShort("拿到好多数据：" + briefListBeen.size)
                                }
                            })
                    )
                Net.getService(MyApi::class.java)
                    .requestList_GSON()
                    .compose(ApplySchedulers())
                    .subscribe(
                        NetObserver(
                            page = this,
                            loadingConfig = this.context?.let { LoadingConfig(it) },
                            callback = object : NetCallBack<List<BriefListBean>>() {
                                override fun onSucceed(
                                    data: List<BriefListBean>,
                                    mothead: String
                                ) {
                                    ToastUtils.showShort("拿到好多数据：" + data.size)
                                }
                            })
                    )
            }
            bt_request_gson_dialog -> {//普通请求，返回Gson 加载数据过程中，不可取消
                Net.getService(MyApi::class.java)
                    .requestList_GSON()
                    .compose(ApplySchedulers())
                    .subscribe(
                        NetObserver(
                            page = this,
                            loadingConfig = this.context?.let { LoadingConfig(it, cancel = false) },
                            callback = object : NetCallBack<List<BriefListBean>>() {
                                override fun onSucceed(
                                    data: List<BriefListBean>,
                                    mothead: String
                                ) {
                                    ToastUtils.showShort("拿到好多数据：" + data?.size)
                                }
                            })
                    )
            }
            bt_request_gson_un_dialog -> {//普通请求，返回Gson 加载数据过程中，不显示Dialog
                Net.getService(MyApi::class.java)
                    .requestList_GSON()
                    .compose(ApplySchedulers())
                    .subscribe(
                        NetObserver(
                            page = this,
                            callback = object : NetCallBack<List<BriefListBean>>() {
                                override fun onSucceed(data: List<BriefListBean>, mothead: String) {
                                    ToastUtils.showShort("拿到好多数据：" + data.size)
                                }
                            })
                    )
            }
            bt_request_gson_buffer -> {//普通请求，返回Gson 加载数据过程中，显示Dialog ->  并缓存，appliction中 初始化的时候可自定义，默认6秒
                Net.getService(MyApi::class.java)
                    .requestList_GSON()
                    .compose(ApplySchedulers()) //多了一个  请求地址的标识 IConstantPool.REQUEST_LIST_URL。因为数据是根据 接口来存，确保唯一性
                    .subscribe(
                        NetObserver(
                            page = this,
                            loadingConfig = this.context?.let { LoadingConfig(it) },
                            cacheConfig = CacheConfig(
                                tag = IConstantPool.REQUEST_LIST_URL,
                                cache = true
                            ),
                            callback = object : NetCallBack<List<BriefListBean>>() {
                                //这个是从本地取的
                                override fun onCookieSucceed(result: String?, mothead: String) {
                                    //这里需要这样解析转换后，返回 给 onSucceed
                                    val type: TypeToken<*> =
                                        object : TypeToken<List<BriefListBean>>() {}
                                    val list: List<BriefListBean> =
                                        Gson().fromJson(result, type.type)
                                    ToastUtils.showShort("缓存区拿的：" + list.size)
                                    //onSucceed(list,mothead);  一般都是公用一个方法
                                }

                                override fun onSucceed(data: List<BriefListBean>, mothead: String) {
                                    ToastUtils.showShort("拿到好多数据：" + data?.size)
                                }
                            })
                    )
            }
            bt_request_gson_buffer_custom -> {//普通请求，返回Gson 加载数据过程中，显示Dialog ->  并缓存，appliction中 初始化的时候可自定义，默认6秒
                Net.getService(MyApi::class.java)
                    .requestList_GSON()
                    .compose(ApplySchedulers()) //多了一个  请求地址的标识 IConstantPool.REQUEST_LIST_URL。因为数据是根据 接口来存，确保唯一性
                    //250 自定义缓存时间，也就是250秒内的请求，直接从 数据库中拿,不进行网络请求
                    .subscribe(
                        NetObserver(
                            page = this,
                            loadingConfig = this.context?.let { LoadingConfig(it) },
                            cacheConfig = CacheConfig(
                                tag = IConstantPool.DOWNLOAD_URL,
                                cache = true,
                                cacheTime = 250
                            ),
                            callback = object : NetCallBack<List<BriefListBean>>() {
                                //这个是从本地取的
                                override fun onCookieSucceed(result: String?, mothead: String) {
                                    //这里需要这样解析转换后，返回 给 onSucceed
                                    val type: TypeToken<*> =
                                        object : TypeToken<List<BriefListBean>>() {}
                                    val list: List<BriefListBean> =
                                        Gson().fromJson(result, type.type)
                                    ToastUtils.showShort("缓存区拿的：" + list.size)
                                    //onSucceed(list,mothead);  一般都是公用一个方法
                                }

                                override fun onSucceed(data: List<BriefListBean>, mothead: String) {
                                    ToastUtils.showShort("拿到好多数据：" + data?.size)
                                }
                            })
                    )
            }
            bt_request_all -> {
                Net.getService(MyApi::class.java)
                    .requestList()
                    .compose(ApplySchedulers<ResponseBody>()) //多了一个  请求地址的标识 IConstantPool.REQUEST_LIST_URL。因为数据是根据 接口来存，确保唯一性     为什么多个 string .. 因为上面demo也是这个接口  不多个标识，不同类，肯定蹦
                    //250 自定义缓存时间，也就是250秒内的请求，直接从 数据库中拿,不进行网络请求
                    .subscribe(
                        NetObserver(
                            page = this,
                            loadingConfig = this.context?.let { LoadingConfig(it) },
                            cacheConfig = CacheConfig(
                                tag = IConstantPool.REQUEST_LIST_URL + ":string",
                                cache = true,
                                cacheTime = 56000
                            ),
                            callback = object : NetCallBack<ResponseBody>() {
                                override fun onSucceed(body: ResponseBody, s: String) {
                                    //这里需要这样解析转换后，返回 给 onSucceed
                                    val result = body.string() //取到json
                                    ToastUtils.showShort("网络拿的：$result")
                                    //切记 切记，因为  ResponseBody的特殊性，只能自己在回调里面存
                                    NetRoomUtil.database.netCacheDao().update(IConstantPool.REQUEST_LIST_URL + ":string", result)
                                }

                                //这个是从本地取的
                                override fun onCookieSucceed(result: String?, mothead: String) {
                                    ToastUtils.showShort("数据库获取的：$result")
                                }
                            })
                    )
            }
        }
    }
}